﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace ToolUltity
{
    public class LoginUser
    {
        public static string USERID = string.Empty;
        public static string USERNAME = string.Empty;
        public static string PWD = string.Empty;
        private readonly string PATH = System.Windows.Forms.Application.StartupPath + "\\Medicarelicense.xml";
        DataSet ds = new DataSet();


        public LoginUser()
        {

        }
        public int checkLogin(string userId,string pwd)
        {
            
            ds.ReadXml(PATH);
            if(ds.Tables.Count<1)
            {
                MessageBox.Show("没有找到配置文件");
            }
            foreach (DataRow row in  ds.Tables["USERSLIST"].Rows)
            {
                if (row["USERID"].ToString().Equals(userId))
                {
                    if (EncryptDecrypt.Decrypt(row["PASSWORD"].ToString()).Equals(pwd))
                    {
                        USERID = userId;
                        PWD = pwd;
                        USERNAME = row["USERNAME"].ToString();
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
                
            }
            return -1;
        }

        public int SaveUser(string USERID, string USERNAME, string NEWPWD)
        {
            ds.ReadXml(PATH);
            foreach (DataRow row in ds.Tables["USERSLIST"].Rows)
            {
                if (row["USERID"].ToString().Equals(USERID))
                {
                    return 1;

                }

            }
            DataRow rownew = ds.Tables["USERSLIST"].NewRow();
            rownew["USERID"] = USERID;
            rownew["USERNAME"] = USERNAME;
            rownew["PASSWORD"] = EncryptDecrypt.Encrypt(NEWPWD);

            ds.Tables["USERSLIST"].Rows.Add(rownew);

            ds.WriteXml(PATH);
            return 0;
        }

        public int UpdateUser(string USERID, string USERNAME, string NEWPWD, string OLDPWD)
        {
            bool exitRecord = false;
            ds.ReadXml(PATH);
            foreach (DataRow row in ds.Tables["USERSLIST"].Rows)
            {
                if (row["USERID"].ToString().Equals(USERID))
                {
                    if (EncryptDecrypt.Decrypt(row["PASSWORD"].ToString()).Equals(OLDPWD))
                    {
                        row.BeginEdit();
                        row["USERID"] = USERID;
                        row["USERNAME"] = USERNAME;
                        row["PASSWORD"] = EncryptDecrypt.Encrypt(NEWPWD);
                        row.EndEdit();
                        ds.WriteXml(PATH);
                        return 0;
                    }
                    else
                    {
                        exitRecord = true;
                        return 1;
                    }


                }

            }
            if (!exitRecord)
            {
                return -1;
            }
            return 0;
        }


        public static bool ReadLicense()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(System.Windows.Forms.Application.StartupPath + "\\Medicarelicense.xml");
            string d = EncryptDecrypt.Decrypt(ds.Tables["LICENSE"].Rows[0]["LICENSEID"].ToString()).Substring(0, 10);
            DateTime de = Convert.ToDateTime(d);
            DateTime now = DateTime.Now;

            if (now > de)
                return false;
            return true;
        }
    }
}
